package com.sinaapp.kltq.dinosaurweather;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinaapp.kltq.data.WeatherDay;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ForecastFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ForecastFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ForecastFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    public static float[] high =new float[6];
    public static float[] low =new float[6];
    public List<WeatherDay> forecastdata;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ForecastFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ForecastFragment newInstance(String param1, String param2) {
        ForecastFragment fragment = new ForecastFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public ForecastFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_forecast, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onStart() {
        WeatherManager wn = new WeatherManager(getActivity().getApplicationContext());

        //get city
        int cityIndex;
        int activityLayoutId;
        if(getActivity().getClass() == FlipWhiteActivity.class){
            cityIndex = ((FlipWhiteActivity)getActivity()).cityIndex;
            activityLayoutId = R.id.whiteTotal;
        }else{
            cityIndex = ((FlipBlackActivity)getActivity()).cityIndex;
            activityLayoutId = R.id.blackTotal;
        }
        String city = wn.getCityList().get(cityIndex).city;
        // 显示背景图
        RelativeLayout layout = ((RelativeLayout) getActivity().findViewById(activityLayoutId));
        if (layout != null) {
            layout.setBackgroundResource(R.drawable.zhexian);
        }

        //input data
         forecastdata = wn.getForecastWeather(city);
        if(forecastdata!=null){

            for(int i = 0;i<6;i++){
                /*String highStr = forecastdata.get(i).high;
                System.out.println("HIGH " + highStr + " VALUEOF(" + highStr.substring(3, highStr.length()-2) + ") is " + Float.valueOf(highStr.substring(3, highStr.length()-1)));*/
                int Hlength = forecastdata.get(i).high.length();
                int Llength = forecastdata.get(i).low.length();
                high[i] = Float.valueOf(forecastdata.get(i).high.substring(3,Hlength-1));
                low[i] = Float.valueOf(forecastdata.get(i).low.substring(3,Llength-1));
            }

            setHigh(high);
            setLow(low);
            ((TextView)getActivity().findViewById(R.id.forecastdata1)).setText(forecastdata.get(0).date.substring(0, 4));
            ((TextView)getActivity().findViewById(R.id.forecastweek1)).setText(forecastdata.get(0).date.substring(5, forecastdata.get(0).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype1)).setText(forecastdata.get(0).dayType);

            ((TextView)getActivity().findViewById(R.id.forecastdata2)).setText(forecastdata.get(1).date.substring(0,4));
            ((TextView)getActivity().findViewById(R.id.forecastweek2)).setText(forecastdata.get(1).date.substring(5,forecastdata.get(1).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype2)).setText(forecastdata.get(1).dayType);

            ((TextView)getActivity().findViewById(R.id.forecastdata3)).setText(forecastdata.get(2).date.substring(0,4));
            ((TextView)getActivity().findViewById(R.id.forecastweek3)).setText(forecastdata.get(2).date.substring(5,forecastdata.get(2).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype3)).setText(forecastdata.get(2).dayType);

            ((TextView)getActivity().findViewById(R.id.forecastdata4)).setText(forecastdata.get(3).date.substring(0,4));
            ((TextView)getActivity().findViewById(R.id.forecastweek4)).setText(forecastdata.get(3).date.substring(5,forecastdata.get(3).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype4)).setText(forecastdata.get(3).dayType);

            ((TextView)getActivity().findViewById(R.id.forecastdata5)).setText(forecastdata.get(4).date.substring(0,4));
            ((TextView)getActivity().findViewById(R.id.forecastweek5)).setText(forecastdata.get(4).date.substring(5,forecastdata.get(4).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype5)).setText(forecastdata.get(4).dayType);

            ((TextView)getActivity().findViewById(R.id.forecastdata6)).setText(forecastdata.get(5).date.substring(0,4));
            ((TextView)getActivity().findViewById(R.id.forecastweek6)).setText(forecastdata.get(5).date.substring(5,forecastdata.get(5).date.length()));
            ((TextView)getActivity().findViewById(R.id.forecastype6)).setText(forecastdata.get(5).dayType);

        }

        super.onStart();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    public void setHigh(float[] high){
        this.high = high;
    }
    public float[] getHigh(){
        return high;
    }
    public void setLow(float[] low){
        this.low = low;
    }
    public float[] getLow(){
        return low;
    }

}
